package com.topnav.mc.admin.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.lib.mybitsplus.JsMapper;
import com.js.lib.mybitsplus.page.JsPage;
import com.topnav.mc.admin.entity.DeviceAlarm;
import com.topnav.mc.admin.entity.DeviceChannel;
import com.topnav.mc.admin.entity.bean.DeviceAlarmBean;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.bean.DeviceChannelBean;
import com.topnav.mc.admin.entity.param.DeviceAlarmParam;
import com.topnav.mc.admin.entity.param.DeviceChannelParam;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.base.mc.McMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: kenny
 * @Date: 2023/11/6 14:29
 */
@Mapper
public interface DeviceAlarmMapper extends McMapper<DeviceAlarm, DeviceAlarmBean, DeviceAlarmParam> {
    String commSql = " SELECT a.*, " +
            " d.code AS deviceCode, d.name AS deviceName,d.longitude AS dLongitude,d.latitude AS dLatitude," +
            " d.coordinate_system AS coordinateSystem, " +
            " o.code AS orgCode, o.name AS orgName, o.short_name AS shortName, o.path," +
            " u.code AS userCode, u.name AS userName " +
            " FROM mc_device_alarm a " +
            " LEFT JOIN mc_device d ON a.device_id = d.id " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " LEFT JOIN mc_user u ON d.user_id = u.id " +
            " ${ew.customSqlSegment}";


    @Select(commSql)
    DeviceAlarmBean selectInfoOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    List<DeviceAlarmBean> selectInfoList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    Page<DeviceAlarmBean> selectInfoPage(Page<DeviceAlarmParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);



    String statisticsSql= "SELECT a.alarm_cate AS alarmCate,COUNT(1) AS total\n" +
            "FROM mc_device_alarm a \n" +
            "LEFT JOIN mc_device d ON a.device_id = d.id\n" +
            "LEFT JOIN mc_organization o ON d.org_id = o.id\n" +
            "LEFT JOIN mc_user u ON d.user_id = u.id\n" +
            " ${ew.customSqlSegment} " +
            "GROUP BY a.alarm_cate";
    /**
     * 按报警类型统计报警数量--分页查询
     */
    @Select(statisticsSql)
    Page<DeviceAlarmBean> countGroupByCatePage(Page<DeviceAlarmParam> page, @Param("ew") Wrapper queryWrapper);

    /**
     * 按报警类型统计报警数量--不分页查询
     */
    @Select(statisticsSql)
    List<DeviceAlarmBean> countGroupByCateList(@Param("ew") Wrapper queryWrapper);
}
